package com.jmyd.coupon.server.domain.po;

import com.jmyd.commons.domain.po.SysUser;
import com.jmyd.coupon.server.domain.dto.SceneDrawCouponDto;
import com.jmyd.coupon.server.domain.dto.SceneGrantCouponDto;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import javax.persistence.Column;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author samphin
 * @since 2021/7/14 16:15
 */
@Data
@Table(name = "scene_coupon_draw_record")
public class SceneCouponDrawRecord {

    /**
     * 用户ID
     */
    @Column(name = "USER_ID")
    private String userId;

    /**
     * 用户账号
     */
    @Column(name = "USERNAME")
    private String username;

    /**
     * 用户昵称
     */
    @Column(name = "NICKNAME")
    private String nickname;

    /**
     * 优惠券ID
     */
    @Column(name = "COUPON_ID")
    private Long couponId;

    /**
     * 领取类型：1、新人注册;2、赠送;3、手动领取
     */
    @Column(name = "`TYPE`")
    private Integer type;

    /**
     * 领取时间
     */
    private Date createTs;

    /**
     * 将Dto转换成po
     *
     * @author samphin
     * @date 2019-8-30 11:19:32
     */
    public List<SceneCouponDrawRecord> build(SceneGrantCouponDto dto, Integer type) {
        //发放时间
        Date drawDate = new Date();
        //批量发放优惠券
        return dto.getUserIds().stream().map(id -> {
            SceneCouponDrawRecord record = new SceneCouponDrawRecord();
            record.setUserId(id);
            record.setCouponId(dto.getCouponId());
            record.setType(type);
            record.setCreateTs(drawDate);
            return record;
        }).collect(Collectors.toList());
    }

    /**
     * 将Dto转换成po
     *
     * @author samphin
     * @date 2019-8-30 11:19:32
     */
    public SceneCouponDrawRecord build(SceneDrawCouponDto dto, SysUser currentUser) {
        //发放时间
        Date drawDate = new Date();
        BeanUtils.copyProperties(dto, this);
        this.setUserId(currentUser.getId());
        this.setUsername(currentUser.getUsername());
        this.setNickname(currentUser.getNickname());
        this.setCreateTs(drawDate);
        this.setType(3);
        return this;
    }

    public SceneCouponDrawRecord build(String userId, Long couponId, Integer type) {
        this.setUserId(userId);
        this.setType(type);
        this.setCouponId(couponId);
        this.setCreateTs(new Date());
        return this;
    }
}